/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */



package com.android1.amarena2d.nodes.sprites;

import com.android1.amarena2d.animation.AtlasAnimation;
import com.android1.amarena2d.engine.Resource;
import com.android1.amarena2d.nodes.behavior.delegates.AnimatedActionTarget;
import com.android1.amarena2d.texture.TextureAtlas;
import com.android1.amarena2d.texture.TextureFrame;

public class AtlasSpriteBatchNode extends SpriteBatchNode<AtlasBatchedSprite> implements AnimatedActionTarget.SetFrame, Resource.Consumer {

    protected final TextureAtlas textureAtlas;

    public AtlasSpriteBatchNode(TextureAtlas textureAtlas) {
        super(textureAtlas.getManagedTexture());
        this.textureAtlas = textureAtlas;
    }

    public AtlasSpriteBatchNode(AtlasAnimation atlasAnimation) {
        this(atlasAnimation.getTextureAtlas());
        animation().setDefaultAnimation(atlasAnimation);
    }

    @Override
    public void init() {
        if (!init) {
            textureAtlas.obtainRef(this);
            textureAtlas.init();
            super.init();
        }
    }

    @Override
    public void dispose() {
        if (init) {
            textureAtlas.releaseRef(this);
            textureAtlas.dispose();
            super.dispose();
        }

    }

    public AtlasBatchedSprite createSpriteBatchEntity() {
        AtlasBatchedSprite entity = new AtlasBatchedSprite(textureAtlas.getTextureFrame(0));
        add(entity);
        return entity;
    }


    public AtlasBatchedSprite createSpriteBatchEntity(TextureFrame textureFrame) {
        AtlasBatchedSprite entity = new AtlasBatchedSprite(textureFrame);
        add(entity);
        return entity;
    }

    public AtlasBatchedSprite createSpriteBatchEntity(int index) {
        AtlasBatchedSprite entity = new AtlasBatchedSprite(textureAtlas.getTextureFrame(index));
        add(entity);
        return entity;
    }

    public AtlasBatchedSprite createSpriteBatchEntity(AtlasAnimation animation) {
        AtlasBatchedSprite entity = new AtlasBatchedSprite(animation);
        add(entity);
        return entity;
    }


    public TextureAtlas getTextureAtlas() {
        return textureAtlas;
    }
}